package com.hxht.storage.server.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.quartz.DateBuilder;

import com.hxht.storage.server.bean.CurrentRecordingBean;
import com.hxht.storage.server.bean.PlanRecordDayTemplateBean;
import com.hxht.storage.server.bean.RecordMarkBean;
import com.hxht.storage.server.bean.TimeIntersection;
import com.fxdigital.nvr.PlanRecordParam;
import com.fxdigital.nvr.SchedTime;
import com.fxdigital.nvr.TimeInfo;
import com.hxht.storage.server.pojo.DayTemplateInfo;
import com.hxht.storage.server.pojo.NvrIpchannelInfo;
import com.hxht.storage.server.pojo.PlanRecordInfo;
import com.hxht.storage.server.pojo.RecordMarkInfo;
import com.hxht.storage.server.pojo.TimePeriodInfo;
import com.hxht.storage.server.pojo.VideoManualRecord;

public class NvrBeanTransUtil {
	
	private static final Logger LOG = Logger.getLogger(NvrBeanTransUtil.class);

	/**  协议类型
    0-HIK，1-PANASONIC，2-SONY，3-AXIS，4-SANYO，5-BOSCH，6-ZAVIO，7-GRANDEYE，
    8-PROVIDEO，9-ARECONT，10-ACTI，11-PELCO，12-VIVOTEK，13-INFINOVA，14-DAHUA，
    32-H264，33-MPEG4，34-SUNELL，35-ATEME，36-LAUNCH，37-YAAN，38-BLUESKY，
    39-BLUESKYLIMIT，40-TDWY，41-HBGK，42-SANTACHI，43-HIGHEASY，44-SAMSUNG，
    64-RTSP(url类型取流)，65-ONVIF 128-CUSTOM 1*/
	private static Map<Integer, String> protocolMap = new HashMap<Integer, String>();
	
	static {
		protocolMap.put(0, "HIKVISION");	protocolMap.put(1, "PANASONIC");
		protocolMap.put(2, "SONY");			protocolMap.put(3, "AXIS");
		protocolMap.put(4, "SANYO");		protocolMap.put(5, "BOSCH");
		protocolMap.put(6, "ZAVIO");		protocolMap.put(7, "GRANDEYE");
		protocolMap.put(8, "PROVIDEO");		protocolMap.put(9, "ARECONT");
		protocolMap.put(10, "ACTI");		protocolMap.put(11, "PELCO");
		protocolMap.put(12, "VIVOTEK");		protocolMap.put(13, "INFINOVA");
		protocolMap.put(14, "DAHUA");		protocolMap.put(32, "H264");
		protocolMap.put(33, "MPEG4");		protocolMap.put(34, "SUNELL");
		protocolMap.put(35, "ATEME");		protocolMap.put(36, "LAUNCH");
		protocolMap.put(37, "YAAN");		protocolMap.put(38, "BLUESKY");
		protocolMap.put(40, "TDWY");		protocolMap.put(39, "BLUESKYLIMIT");	
		protocolMap.put(41, "HBGK");		protocolMap.put(42, "SANTACHI");
		protocolMap.put(43, "HIGHEASY");	protocolMap.put(44, "SAMSUNG");
		protocolMap.put(64, "RTSP");		protocolMap.put(65, "ONVIF");
		protocolMap.put(128, "CUSTOM 1");	
	}
	/** 
	 * 根据传入的 ip通道协议值，返回对应的协议描述字符串
	 * @param protocol
	 * @return
	 */
	public static String getIpchannelProtocol(int protocol){
		return protocolMap.get(protocol);
	}
	
	/**
	 * 将 客户端传入的 SchedTime对象 转换为 数据库中存储的 TimePeriodInfo对象
	 * @param bean
	 * @return
	 */
	public static TimePeriodInfo transSchedTimeToTimePeriodInfo(SchedTime bean){
		
		if (bean == null)
			return null;
		
		TimePeriodInfo info = new TimePeriodInfo();
		
		info.setStartHour(bean.getStartHour());
		info.setStartMinute(bean.getStartMinute());
		
		info.setStopHour(bean.getStopHour());
		info.setStopMinute(bean.getStopMinute());
		
		info.setType(bean.getType());
		
		return info;
	}
	
	public static void cloneIpchannelInfo(NvrIpchannelInfo from,
			NvrIpchannelInfo to) {
		to.setDeviceChannel(from.getDeviceChannel());
		to.setDvrChannel(from.getNvrChannel());
		to.setIp(from.getIp());
		to.setIsIdle(from.isIdle());
		to.setPassword(from.getPassword());
		to.setPort(from.getPort());
		to.setProtocol(from.getProtocol());
		to.setStreamMode(from.getStreamMode());
		to.setUserName(from.getUserName());
		to.setIsOnline(from.isOnline());
	}

	/**
	 * 将 数据库中 计划录像的 每天木板DayTemplateInfo 转化为 nvr.dll 计划录像JNI调用时的计划录像参数 PlanRecordParam
	 * @param dayTemplate
	 * @return
	 */
	public static PlanRecordParam getPlanRecordParamFromDayTemplate(
			DayTemplateInfo dayTemplate) {
		
		PlanRecordParam param = new PlanRecordParam();
		
		if (dayTemplate == null)
			return param;
		
		if (dayTemplate.isWholeDayMode()){
			param.setMode(1);
			param.setType(dayTemplate.getRecordType());
			return param;
		} else {
			param.setMode(0);
		}
		
		if (dayTemplate.getSchedTimeList() == null)
			return param; 
		
		Iterator<TimePeriodInfo> timeIterator = dayTemplate.getSchedTimeList().iterator();
		int i = 1;
		
		while (timeIterator.hasNext()){
			TimePeriodInfo time = timeIterator.next();
			if (i == 1)
				param.setSchedTime1(transTimePeriodInfoToSchedTime(time));
			
			if (i == 2)
				param.setSchedTime2(transTimePeriodInfoToSchedTime(time));
			
			if (i == 3)
				param.setSchedTime3(transTimePeriodInfoToSchedTime(time));
			
			if (i == 4)
				param.setSchedTime4(transTimePeriodInfoToSchedTime(time));
			
			if (i == 5)
				param.setSchedTime5(transTimePeriodInfoToSchedTime(time));
			
			if (i == 6)
				param.setSchedTime6(transTimePeriodInfoToSchedTime(time));
			
			if (i == 7)
				param.setSchedTime7(transTimePeriodInfoToSchedTime(time));
			
			// 最多只允许设置8个时间片段
			if (i == 8){
				param.setSchedTime8(transTimePeriodInfoToSchedTime(time));
				break;
			}
			
			i++;
		}
			
		return param;
	}

	/**
	 * 将 数据库中的 DayTemplateInfo对象 转换为 返回给客户端查看的PlanRecordDayTemplateBean对象
	 * @param dayTemplate
	 * @return
	 */
	public static PlanRecordDayTemplateBean transDayTemplateInfoToDayTemplateBean(
			DayTemplateInfo dayTemplate) {
		
		PlanRecordDayTemplateBean bean = new PlanRecordDayTemplateBean();
		
		bean.setPlanMode(dayTemplate.isWholeDayMode() ? 1 : 0);
		bean.setRecordtype(dayTemplate.getRecordType());
		
		List<SchedTime> schedTimeList = new ArrayList<SchedTime>();
		for (TimePeriodInfo period:dayTemplate.getSchedTimeList()){
			schedTimeList.add(transTimePeriodInfoToSchedTime(period));
		}
		
		bean.setSchedTimeList(schedTimeList);
		
		
		return bean;
	}
	/**
	 * 将数据库中的时间片段信息TimePeriodInfo 转化为 nvr.dll 计划录像JNI调用时的SchedTime信息
	 * @param recordType
	 * @param time
	 * @return
	 */
	private static SchedTime transTimePeriodInfoToSchedTime(TimePeriodInfo time) {
		
		SchedTime schedTime = new SchedTime();
		
		schedTime.setType(time.getType());
		schedTime.setStartHour(time.getStartHour());
		schedTime.setStartMinute(time.getStartMinute());
		
		schedTime.setStopHour(time.getStopHour());
		schedTime.setStopMinute(time.getStopMinute());
		
		return schedTime;
	}
	
	/** 
	 * 将jni调用nvr.dll返回的当前nvr 系统时间信息TimeInfo转化为
	 * Date对象 
	 * @param timeInfo
	 * @return
	 */
	public static Date transTimeInfoToDate(TimeInfo timeInfo) {
		// TODO Auto-generated method stub
		Date date = DateBuilder.dateOf(timeInfo.getHour(), timeInfo.getMinute(), 
				timeInfo.getSecond(), timeInfo.getDay(), timeInfo.getMonth(), timeInfo.getYear());
		
		return date;
	}
	
	/** 
	 * 将Date，转化为 yyyy-MM-dd HH:mm:ss格式的字符串
	 * @param date
	 * @return
	 */
	public static String transDateToFormatString(Date date){
		
		if (date == null)
			return null;
			
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		return dateFormat.format(date);
	}

	/** 
	 * 将 yyyy-MM-dd HH:mm:ss格式的字符串, 转化为Date
	 * @param 
	 * @return
	 */
	public static Date transStringToDate(String formatString){
		
		Date date = null;
		
		if (StringUtils.isEmpty(formatString))
			return null;
			
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		try {
			date = dateFormat.parse(formatString);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			LOG.warn("date format string is error! formatString = " + formatString);
		}
		
		return date;
	}

	/**
	 * 将录像标注的 RecordMarkInfo 转换为 RecordMarkBean
	 * @param mark
	 * @return
	 */
	public static RecordMarkBean transMarkInfoToMarkBean(RecordMarkInfo mark) {
		
		if (mark == null)
			return null;
		
		RecordMarkBean markBean = new RecordMarkBean();
		
		markBean.setMarkName(mark.getMarkName());
		markBean.setMarkContent(mark.getMarkContent());
		markBean.setMarkTime(transDateToFormatString(mark.getMarkTime()));
		markBean.setUuid(mark.getUuid());
		
		return markBean;
	}

	/** 
	 * 将 Date 转化为 TimeInfo 
	 * @param time
	 * @return
	 */
	public static TimeInfo transDateToTimeInfo(Date time) {
		
		Calendar calendar = GregorianCalendar.getInstance();
		
		calendar.setTime(time);
		
		if (time == null)
			return null;
		
		TimeInfo timeInfo = new TimeInfo();
		timeInfo.setYear(calendar.get(Calendar.YEAR));
		
		// TimeInfo的 month从1开始，Date的month从0开始，所以要 + 1
		timeInfo.setMonth(calendar.get(Calendar.MONTH) + 1);
		timeInfo.setDay(calendar.get(Calendar.DAY_OF_MONTH));
		timeInfo.setHour(calendar.get(Calendar.HOUR_OF_DAY));
		
		timeInfo.setMinute(calendar.get(Calendar.MINUTE));
		timeInfo.setSecond(calendar.get(Calendar.SECOND));
		
		return timeInfo;
	}

	public static final String RECORDING_TYPE_MANUAL = "手工录像";
	public static final String RECORDING_TYPE_PLAN = "计划录像";
	
	public static CurrentRecordingBean transManualRecordingToRecordingBean(
			VideoManualRecord recording) {
		
		if (recording == null || !recording.getIsRecoding()){
			return null;
		}
		
		CurrentRecordingBean bean = new CurrentRecordingBean();
		
		NvrIpchannelInfo ipchannel = recording.getIpChannel();
		
		
		bean.setDeviceId(recording.getDeviceId());
		bean.setDeviceChannelIndex(recording.getDeviceChannel());
//		bean.setOnline(ipchannel.isOnline());
		bean.setNvrIp(ipchannel.getNvrDevice().getIp());
		bean.setIpchannel(ipchannel.getNvrChannel());
		bean.setType(RECORDING_TYPE_MANUAL);
		
		return bean;
	}
	
	public static CurrentRecordingBean transPlanRecordingToRecordingBean(
			PlanRecordInfo recording) {
		
		if (recording == null || !recording.getIsRecoding()){
			return null;
		}
		
		CurrentRecordingBean bean = new CurrentRecordingBean();
		
		NvrIpchannelInfo ipchannel = recording.getIpChannel();
		
		
		bean.setDeviceId(recording.getDeviceId());
		bean.setDeviceChannelIndex(recording.getDeviceChannel());
//		bean.setOnline(ipchannel.isOnline());
		bean.setNvrIp(ipchannel.getNvrDevice().getIp());
		bean.setIpchannel(ipchannel.getNvrChannel());
		bean.setType(RECORDING_TYPE_PLAN);
		bean.setPlanTemplateName(recording.getWeekTemplate().getTemplateName());
		
		return bean;
	}
	
}
